﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FanPages.Server.Services.Contracts;
using FanPages.Server.Services;
using FanPages.Model;
using FanPages.Client.Core.Transformers;
using FanPages.Server.Services.Contracts.Dto;

namespace FanPagesWeb.Controllers
{
    public class SearchExceptionController : Controller
    {
        ISearchExceptionService service = new SearchExceptionService();

        public ActionResult Index()
        {
            var fanItem = new FanItemTransformer().ToDto(Session["FanItem"] as FanItem);
            return View(new SearchExceptionTransformer().ToListEntity(service.GetByFanItem(fanItem)));
        }
        

        public ActionResult Details(int id)
        {
            return View(new SearchExceptionTransformer().ToClient(service.GetById(id)));
        }



        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /SearchException/Create

        [HttpPost]
        public ActionResult Create(SearchException searchException)
        {
            try
            {
                var fanItem = Session["FanItem"] as FanItem;
                searchException.FanItem = fanItem;
                service.Save(new SearchExceptionTransformer().ToDto(searchException));
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /SearchException/Edit/5

        public ActionResult Edit(int id)
        {

            return View(new SearchExceptionTransformer().ToClient(service.GetById(id)));
        }

        //
        // POST: /SearchException/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, SearchException searchException)
        {
            try
            {
                var fanItem = Session["FanItem"] as FanItem;
                searchException.FanItem = fanItem;
                service.Save(new SearchExceptionTransformer().ToDto(searchException));                
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /SearchException/Delete/5

        public ActionResult Delete(int id)
        {
            return View(new SearchExceptionTransformer().ToClient(service.GetById(id)));
        }

        //
        // POST: /SearchException/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, SearchException searchException)
        {
            try
            {
                
                var dto = new SearchExceptionDto() { Id = searchException.Id};
                service.Delete(dto);     
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
